import { setToken, setRefreshToken } from '/@/util/auth'
import { getStore, setStore } from '/@/util/store'
import { loginByMobile, loginByUsername, getUserInfo, logout, refreshToken } from '/@/api/login1'
import { deepClone, encryption } from '/@/util'
import { formatPath } from '/@/router/avue-router'
import { getMenu } from '/@/api/admin/menu'
import { Session } from '/@/util/storage';
const user = {
  state: {
    userInfo:
      getStore({
        name: 'userInfo'
      }) || {},
    permissions:
      getStore({
        name: 'permissions'
      }) || [],
    roles: [],
    menu:
      getStore({
        name: 'menu'
      }) || [],
    menuAll: getStore({ name: 'menuAll' }) || [],
    access_token:
      getStore({
        name: 'access_token'
      }) || '',
    refresh_token:
      getStore({
        name: 'refresh_token'
      }) || '',
    orderCount: '',
    complainCount: '',
    taskObj: {
      user: []
    },
    step1: {
      logo: '',
      name: '',
      flowId: '',
      groupId: undefined,
      adminList: [],
      remark: '',
    },
    step2: [],
    promoterDrawer: false,
    approverDrawer: false,
    approverConfigData: {},
    starterConfigData: {},
    copyerDrawer: false,
    copyerConfig1: {},
    conditionDrawer: false,
    conditionsConfig1: {
      conditionNodes: [],
    },
    isLockFlag: false || Session.get('isLockFlag'),
  },
  actions: {
    // 根据用户名登录
    LoginByUsername({ commit }, userInfo) {
      const user = encryption({
        data: userInfo,
        key: 'thanks,pig4cloud',
        param: ['password']
      })
      return new Promise((resolve, reject) => {
        loginByUsername(user.username, user.password, user.code, user.randomStr)
          .then((response) => {
            const data = response.data
            commit('SET_ACCESS_TOKEN', data.access_token)
            commit('SET_REFRESH_TOKEN', data.refresh_token)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 根据手机号登录
    LoginByPhone({ commit }, smsForm) {
      return new Promise((resolve, reject) => {
        loginByMobile(smsForm)
          .then((response) => {
            const data = response.data
            commit('SET_ACCESS_TOKEN', data.access_token)
            commit('SET_REFRESH_TOKEN', data.refresh_token)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },

    // 刷新token
    RefreshToken({ commit, state }) {
      return new Promise((resolve, reject) => {
        refreshToken(state.refresh_token)
          .then((response) => {
            const data = response.data
            commit('SET_ACCESS_TOKEN', data.access_token)
            commit('SET_REFRESH_TOKEN', data.refresh_token)
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 查询用户信息
    GetUserInfo({ commit }) {
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then((res) => {
            const data = res.data.data || {}
            commit('SET_USER_INFO', data.sysUser)
            commit('SET_ROLES', data.roles || [])
            commit('SET_PERMISSIONS', data.permissions || [])
            resolve(data)
          })
          .catch(() => {
            reject()
          })
      })
    },
    // 登出
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout()
          .then(() => {
            commit('SET_MENUALL_NULL', [])
            commit('SET_MENU', [])
            commit('SET_PERMISSIONS', [])
            commit('SET_USER_INFO', {})
            commit('SET_ACCESS_TOKEN', '')
            commit('SET_REFRESH_TOKEN', '')
            commit('SET_ROLES', [])
            commit('DEL_ALL_TAG')
            commit('DEL_MENU_DATA')
            commit('CLEAR_LOCK')
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    // 注销session
    FedLogOut({ commit }) {
      return new Promise((resolve) => {
        commit('SET_MENU', [])
        commit('SET_MENUALL_NULL', [])
        commit('SET_PERMISSIONS', [])
        commit('SET_USER_INFO', {})
        commit('SET_ACCESS_TOKEN', '')
        commit('SET_REFRESH_TOKEN', '')
        commit('SET_ROLES', [])
        commit('DEL_ALL_TAG')
        commit('DEL_MENU_DATA')
        commit('CLEAR_LOCK')
        resolve()
      })
    },
    // 获取系统菜单
    GetMenu({ commit }, obj = {}) {
      // 记录用户点击顶部信息，保证刷新的时候不丢失
      commit('LIKE_TOP_MENUID', obj)
      return new Promise((resolve) => {
        if (obj.menu) {
          const menu = deepClone(obj.menu)
          menu.forEach((ele) => formatPath(ele, true))
          commit('SET_MENUALL', menu)
          commit('SET_MENU', menu)
          resolve(menu)
        } else {
          //console.log(obj);
          getMenu(obj.id).then((res) => {
            const data = res.data.data
            //console.log(data);
            const menu = deepClone(data)
            //console.log(menu);
            menu.forEach((ele) => formatPath(ele, true))
            commit('SET_MENUALL', menu)
            commit('SET_MENU', menu)
            resolve(menu)
          })
        }
      })
    },
    //顶部菜单
    GetTopMenu() {
      return new Promise((resolve) => {
        resolve([])
      })
    }
  },
  mutations: {
    setLockState(state, val) {
      state.isLockFlag = val
    },
    changeStep1: (state, p) => {
      state.step2 = p;
    },
    setStep2: (state, p) => {
      state.step2 = p;
    },
    clearStep1: (state) => {
      state = {
        logo: '',
        name: '',
        flowId: '',
        groupId: undefined,
        adminList: [],
        remark: '',
      };
    },
    clearStep2: (state) => {
      state.step2 = [];
    },
    taskphoto: (state, val) => {
      // state.taskObj.user=val
    },
    changeUser: (state, val) => {
      state.taskObj.user = val
    },
    SET_ACCESS_TOKEN: (state, access_token) => {
      state.access_token = access_token
      setToken(access_token)
      setStore({
        name: 'access_token',
        content: state.access_token,
        type: 'session'
      })
    },
    SET_REFRESH_TOKEN: (state, rfToken) => {
      state.refresh_token = rfToken
      setRefreshToken(rfToken)
      setStore({
        name: 'refresh_token',
        content: state.refresh_token,
        type: 'session'
      })
    },
    SET_USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo
      setStore({
        name: 'userInfo',
        content: userInfo,
        type: 'session'
      })
    },
    SET_MENUALL: (state, menuAll) => {
      const menu = state.menuAll
      menuAll.forEach((ele) => {
        if (!menu.find((item) => item.label === ele.label && item.path === ele.path)) {
          menu.push(ele)
        }
      })
      state.menuAll = menu
      setStore({ name: 'menuAll', content: state.menuAll })
    },
    SET_MENUALL_NULL: (state) => {
      state.menuAll = []
      setStore({ name: 'menuAll', content: state.menuAll })
    },
    SET_MENU: (state, menu) => {
      state.menu = menu
      setStore({ name: 'menu', content: state.menu })
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      const list = {}
      for (let i = 0; i < permissions.length; i++) {
        list[permissions[i]] = true
      }

      state.permissions = list
      setStore({
        name: 'permissions',
        content: list,
        type: 'session'
      })
    },
    setOrderCount: (state, val) => {
      state.orderCount = val
    },
    setComplainCount: (state, val) => {
      state.complainCount = val
    }
  }
}
export default user
